package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.game.activity.newplayer.ActivityNewPlayerService;
import com.xcity.game.activity.newplayer.NewPlayerShopTemplate;
import com.xcity.game.activity.newplayer.NewPlayerTask;
import com.xcity.game.activity.newplayer.NewPlayerTask.State;
import com.xcity.game.activity.newplayer.NewPlayerTaskTemplate;
import com.xcity.game.activity.newplayer.NewPlayerTracer;
import com.xcity.game.activity.newplayer.NewPlayerTracerFactory;

import gnu.trove.map.hash.TIntObjectHashMap;
import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.game.time.TimeUpdater;
import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;
import naga.x.util.TimeUtils;

@PomeloMessage(type = Message.Type.RESPONSE)
public class ActivityNewPlayerInfoResponse extends Response {
//	public List<List<DayActiveItem>> returnList;
	public List<TabActiveList> returnList;
//	public List<DayShopItem> dayShopList;
//	public List<HalfShopItem> halfShopList;
	private int currentDay;
	private long secondsToCountEnd;
	private long secondsToRewardEnd;
	private int day7reward;

	public int getCurrentDay() {
		return currentDay;
	}
	public void setCurrentDay(int currentDay) {
		this.currentDay = currentDay;
	}
	public long getSecondsToCountEnd() {
		return secondsToCountEnd;
	}
	public void setSecondsToCountEnd(long secondsToCountEnd) {
		this.secondsToCountEnd = secondsToCountEnd;
	}
	public long getSecondsToRewardEnd() {
		return secondsToRewardEnd;
	}

	public void setSecondsToRewardEnd(long secondsToRewardEnd) {
		this.secondsToRewardEnd = secondsToRewardEnd;
	}
	
	public static class TabActiveList{
		public int tabType;
		public List<DayActiveItem> activeList;
		public TabActiveList(int tab, List<DayActiveItem> list){
			this.tabType = tab;
			this.activeList = list;
		}
	}
	
	public static class ItemSimpleInfo{
		public int itemId;
		public int itemCount;
		public ItemSimpleInfo(int itemId, int itemCount){
			this.itemId = itemId;
			this.itemCount = itemCount;
		}
	}
	
	/**
	 * 每天的活动目标条目
	 */
	public static class DayActiveItem{
		public int activeIndex;
		public int startDay;
		public String doingDesc;
		public int rewardMoney;
		public int rewardStone;
		public byte state;//0未完成 1完成未领 2已领取
		public ItemSimpleInfo[] items;
		public int type;
		public int[] params;
//		public String[] rewardNames;
//		public String[] rewardIcons;
//		public int[] rewardCounts;
		public DayActiveItem(NewPlayerTaskTemplate temp, String desc, byte state){
			this.activeIndex = temp.getId();
			this.startDay = temp.getStartDay();
			this.doingDesc = desc;
			this.state = state;
			this.rewardMoney = temp.getMoney();
			this.rewardStone = temp.getStone();
			int size = temp.getItemIds().length;
			this.items = new ItemSimpleInfo[size];
			for(int i = 0; i < size; i++){
				this.items[i] = new ItemSimpleInfo(temp.getItemIds()[i], temp.getItemCounts()[i]);
			}
			this.type = temp.getType();
			this.params = temp.getParam();
//			this.rewardNames = new String[size];
//			this.rewardIcons = new String[size];
//			this.rewardCounts = new int[size];
//			if(size > 3){
//				size = 3;
//			}
//			TemplateService ts = App.getApp().getServiceManager().get(TemplateService.class);
//			for(int i = 0; i < size; i++){
//				ItemTemplate itemTemp = ts.get(ItemTemplate.class, temp.getItemIds()[i]);
//				if(itemTemp == null){
//					rewardNames[i] = "";
//					rewardIcons[i] = "";
//					rewardCounts[i] = 0;
//				}else{
//					rewardNames[i] = itemTemp.getName();
//					rewardIcons[i] = itemTemp.getIcon();
//					rewardCounts[i] = temp.getItemCounts()[i];
//				}
//			}
					
		}
	}
	
	/**
	 * 每日热卖条目
	 */
	public static class DayShopItem{
		public int activeIndex;
		public int id;//商品ID
		public int count;//商品数量
//		public String itemIcon;
//		public String itemName;
		public int price;
		public int buyCount;//已购买的次数
		public int totalCount;//总共购买次数
		public int discount;//折扣
		public DayShopItem(NewPlayerTask task){
			this.activeIndex = task.getTemplate().getId();
//			this.itemIcon = "";
//			this.itemName = "";
			this.buyCount = task.getProgress()[0];
			
			TemplateService ts = App.getApp().getServiceManager().get(TemplateService.class);
			NewPlayerShopTemplate shopItemTemp = ts.get(NewPlayerShopTemplate.class, task.getTemplate().getParam()[0]);
			if(shopItemTemp != null){
				this.price = shopItemTemp.getPrice();
				this.count = shopItemTemp.getItemCount();
				this.totalCount = shopItemTemp.getCount();
				this.id = shopItemTemp.getItemId();
//				ItemTemplate itemTemp = ts.get(ItemTemplate.class, shopItemTemp.getItemId());
//				if(itemTemp != null){
//					this.itemIcon = itemTemp.getIcon();
//					this.itemName = itemTemp.getName();
//				}
				this.discount = shopItemTemp.getDiscount();
			}
		}
	}
	/**
	 * 半价抢购条目
	 */
	public static class HalfShopItem{
		public int activeIndex;
		public int id;//商品ID
		public int count;//商品数量
//		public String itemIcon;
//		public String itemName;
		public int price;
		public int buyCount;//已购买的次数
		public int totalCount;//总共购买次数
		public int original;//原价
		public HalfShopItem(NewPlayerTask task){
			this.activeIndex = task.getTemplate().getId();
//			this.itemIcon = "";
//			this.itemName = "";
			this.buyCount = task.getProgress()[0];
			
			TemplateService ts = App.getApp().getServiceManager().get(TemplateService.class);
			NewPlayerShopTemplate shopItemTemp = ts.get(NewPlayerShopTemplate.class, task.getTemplate().getParam()[0]);
			if(shopItemTemp != null){
				this.price = shopItemTemp.getPrice();
				this.count = shopItemTemp.getItemCount();
				this.totalCount = shopItemTemp.getCount();
				this.id = shopItemTemp.getItemId();
//				ItemTemplate itemTemp = ts.get(ItemTemplate.class, shopItemTemp.getItemId());
//				if(itemTemp != null){
//					this.itemIcon = itemTemp.getIcon();
//					this.itemName = itemTemp.getName();
//				}
				this.original = shopItemTemp.getOriginal();
			}
		}
	}

	public ActivityNewPlayerInfoResponse(int serial, Player player, int day, int day7reward) {
		super(serial);
		int today = TimeUpdater.getInstance().today();
		this.currentDay = TimeUtils.getDayGap(player.getNewPlayerTasks().getCreatDay(), today) + 1;//today - player.getNewPlayerTasks().getCreatDay() + 1;
		this.secondsToCountEnd = player.getNewPlayerTasks().getCountEndSecond();
		this.secondsToRewardEnd = player.getNewPlayerTasks().getRewardEndSecond();		
		this.day7reward = day7reward;
		returnList = new ArrayList<TabActiveList>();
//		for(int i = 1; i <= ActivityNewPlayerService.COUNT_DAY; i++){
//			if(this.currentDay >= i){
//			}
//		}
			List<NewPlayerTask> taskList = player.getNewPlayerTasks().getTaskListByDay(day);
//			List<DayActiveItem> dayList = new ArrayList<DayActiveItem>();
			TIntObjectHashMap<TabActiveList> tab2actilist = new TIntObjectHashMap<TabActiveList>();
			for(int j = 0; j < taskList.size(); j++){
				NewPlayerTask task = taskList.get(j);
				byte type = task.getTemplate().getType();
				byte tab = task.getTemplate().getTab();
				if(task.getTemplate().getBan() != 1){
					if(type == ActivityNewPlayerService.FUNC_TYPE_DAY_SHOP){
//							DayShopItem dayShopItem = new DayShopItem(task);
//							dayShopList.add(dayShopItem);
					}else if(type == ActivityNewPlayerService.FUNC_TYPE_HALF_SHOP){
						if(this.currentDay == day){
//							HalfShopItem halfShopItem = new HalfShopItem(task);
//							halfShopList.add(halfShopItem);
						}
					}else{
						NewPlayerTracer tracer = NewPlayerTracerFactory.creat(type);
						byte state = 0;
						if(task.getState() == State.DONE){
							state = 1;
						}else if(task.getState() == State.GOT){
							state = 2;
						}
						DayActiveItem item = new DayActiveItem(task.getTemplate(), tracer.getDoingStr(task), state);
						
						TabActiveList activeList = null;
						if(!tab2actilist.containsKey(tab)){
							activeList = new TabActiveList(tab, new ArrayList<DayActiveItem>());
							returnList.add(activeList);
							tab2actilist.put(tab, activeList);
						}else{
							activeList = tab2actilist.get(tab);
						}
						activeList.activeList.add(item);
//						dayList.add(item);
					}
				}
			}
			returnList.sort(new Comparator<TabActiveList>() {
				@Override
				public int compare(TabActiveList o1, TabActiveList o2) {
					return o1.tabType - o2.tabType;
				}
			});
			for(int i = returnList.size() - 1; i > -1; i--){
				returnList.get(i).activeList.sort(new Comparator<DayActiveItem>() {
					@Override
					public int compare(DayActiveItem o1, DayActiveItem o2) {
						if(o1.state == 1){
							return -1;
						}else if(o2.state == 1){
							return 1;
						}
						return o1.state - o2.state;
					}
				});
			}
//			if(dayList.size() > 0){
//				returnList.add(dayList);
//			}
	}
	public int getDay7reward() {
		return day7reward;
	}
	public void setDay7reward(int day7reward) {
		this.day7reward = day7reward;
	}
}
